<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\controller\Frontend;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Order extends Backend
{

    /**
     * Order模型对象
     * @var \app\admin\model\Order
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Order;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("typeslistList", $this->model->getTypeslistList());
    }



    public function downzip(){


        $ids=$this->request->param('ids');

        $order=Db::name('order')->where(['id'=>$ids])->find();

        $files=explode(',',$order['images']);
            //zip压缩包保存路径


        $downfile="/uploads/one/".$order['ordersn'].".zip";

        $filename= ROOT_PATH.'public'.$downfile;

        $zip=new \ZipArchive();

        // 打开压缩包
        if ($zip->open($filename, \ZipArchive::CREATE) !== TRUE) {

            exit("无法打开 <$filename>\n");
        }


        foreach ($files as $key){
            $fileToAdd = '.'.$key;
            $zip->addFile($fileToAdd, basename($fileToAdd));
        }

        $zip->close();


        return $this->success('压缩成功，请不要关闭浏览器','',['file'=>cdnurl($downfile,true)]);

    }


//    public function addFileToZip($zip,$path,$root){
//
//        $handler=opendir($path); //打开当前文件夹
//        while(($filename=readdir($handler))!==false){
//
//            if($filename != "." && $filename != ".."){
//                //不操作名字为'.'和'..'的文件夹或文件
//                if(is_dir($path."/".$filename)){
//                    // 如果读取的某个对象是文件夹，则递归
//                    addFileToZip($zip,$path."/".$filename,$root);
//                }else{
//                    //将文件加入zip对象，第二个参数是zip里文件的路径
//                    $pathFilename=$path . "/" . $filename;
//                    $zip->addFile($pathFilename, str_replace($root.'/','',$pathFilename));
//                }
//            }
//        }
//        @closedir($path);
//    }




}
